package com.plug.common.spring.utils;

import com.plug.common.quartz.QuartzManager;
import com.plug.common.quartz.job.QuartzJob;
import com.plug.entity.PlugContrast;
import com.plug.service.IPlugContrastService;
import org.quartz.SchedulerException;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.List;

/**
 * Created by slayes on 2017/1/16.
 */
public class InstantiationTracingBeanPostProcessor implements ApplicationListener<ContextRefreshedEvent> {

    public void onApplicationEvent(ContextRefreshedEvent event) {
        if(event.getApplicationContext().getParent() == null){//root application context 没有parent，他就是老大.
            IPlugContrastService plugContrastService = (IPlugContrastService) ApplicationContextHelper.getBean("plugContrastService");
            List<PlugContrast> list = plugContrastService.findTaskAll();
            try {
                for(PlugContrast plugContrast : list){
                    QuartzManager.addJob(plugContrast.getTaskId(), plugContrast.getTaskId(), QuartzJob.class, plugContrast.getTimer());
                }
                QuartzManager.startAll();
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
        }
    }
}
